package com.future.bantang.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.future.bantang.R;
import com.future.bantang.activity.ShoppingListActivity;
import com.future.bantang.adapter.UserRecommendAdapter;
import com.future.bantang.constants.BanTang;
import com.future.bantang.entity.UserRecommendEntity;
import com.future.bantang.utils.JsonUtils;
import com.future.bantang.utils.L;
import com.future.bantang.utils.NetUtils;
import com.future.bantang.view.ScrollMonitorableScrollView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by Administrator on 16-1-20.
 */
public class UserRecommendFragment extends BaseFragment {
    @ViewInject(R.id.lv_user_recommend_shopping_list)
    private ListView mLvUserRecommend;

    private UserRecommendAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_user_recommend_shopping_list, null);
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);

        mLvUserRecommend.setFocusable(false);
        mAdapter=new UserRecommendAdapter(getActivity());
        mLvUserRecommend.setAdapter(mAdapter);


        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void getUserRecommendArgemnt(ShoppingListActivity.UserRecommendMsg msg){
        tagIds=msg.getT();
        topicId=msg.getStr();
        final ScrollMonitorableScrollView scrollView=msg.getmScrollView();
        L.d("topicId="+topicId+",tagIds="+tagIds);
        loadUserRecommendDatas();

        mLvUserRecommend.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem == 0) {
                    View topView = view.getChildAt(0);
                    L.d("view=" + topView);
                    if (topView.getTop() >= 0) {
                        if (scrollView != null) {
                            scrollView.setIsIntercapt(true);
                        }
                    } else {
                        if (scrollView != null) {
                            scrollView.setIsIntercapt(false);
                        }
                    }
                } else {
                    if (scrollView != null) {
                        scrollView.setIsIntercapt(false);
                    }
                }
            }
        });
    }


    private int pageIndex=0;
    private int pageSize=10;
    private int productNum=1;
    //shoppinglist数据中的tag_ids字段
    private String tagIds="8013";
    private String typeId="1";
    //介绍和精选的接口的id
    private String topicId="1874";

    @Override
    protected void loadDatas() {
        super.loadDatas();
    }

    private void loadUserRecommendDatas(){
        final RequestParams requestParams = new NetUtils.RequestParamsBuilder()
                .addPostParameter(BanTang.CLIENT_ID_KEY, BanTang.CLIENT_ID_VALUE)
                .addPostParameter(BanTang.CLIENT_SECRET_KEY, BanTang.CLIENT_SECRET_VALUE)
                .addPostParameter(BanTang.PAGE, pageIndex + "")
                .addPostParameter(BanTang.PAGESIZE, pageSize + "")
                .addPostParameter(BanTang.TAG_IDS, tagIds)
                .addPostParameter(BanTang.TYPE_ID, typeId)
                .addPostParameter(BanTang.TOPIC_ID, topicId)
                .addPostParameter(BanTang.SHOW_PRODUCT, productNum + "")
                .build();
        NetUtils.loadUserRecommendDatas(requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                L.d("User Recommend","network success,size="+responseInfo.result.length());
                try {
                    UserRecommendEntity userRecommendEntity = JsonUtils
                            .perse(responseInfo.result, UserRecommendEntity.class);
                    mAdapter.addDatas(userRecommendEntity.getData().getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Log.e("network failed", error.getMessage());
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
